#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 26;
const int INF = 0x3f3f3f3f;

int n, m;
int g[N][N];
int d[N][N], res[N][N];
void floyd() {
  rep(k, 0, 25) rep(i, 0, 25) rep(j, 0, 25) {
    g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
  }
}
void init() {
  rep(i, 0, 25) rep(j, 0, 25) {
    res[i][j] = -1;
    rep(k, 0, 25) {
      if (g[i][k] >= INF || g[j][k] >= INF) continue;
      if (res[i][j] == -1 || d[i][j] > g[i][k] + g[j][k]) {
        res[i][j] = k;
        d[i][j] = g[i][k] + g[j][k];
      }
    }
  }
}
void solve() {
  string s, t;
  cin >> s >> t;
  if (s.size() != t.size()) {
    cout << -1 << endl;
    return;
  }
  cin >> n;
  memset(g, 0x3f, sizeof(g));
  rep(i, 0, 25) g[i][i] = 0;
  rep(i, 1, n) {
    char c, d;
    int w;
    cin >> c >> d >> w;
    int x = c - 'a', y = d - 'a';
    g[x][y] = min(g[x][y], w);
  }
  floyd();
  init();
  ll cost = 0;
  string ans;
  rep(i, 0, s.size() - 1) {
    int x = s[i] - 'a', y = t[i] - 'a';
    if (res[x][y] == -1) {
      cout << -1 << endl;
      return;
    }
    ans.push_back(res[x][y] + 'a');
    cost += d[x][y];
  }
  cout << cost << endl << ans;
  return;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  t = 1;
  while (t--) solve();
  return 0;
}